package com.mask.order.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mask.order.entity.Order;
import com.mask.order.service.OrderService;
import com.mask.roll.common.code.ServiceEnums;
import com.mask.roll.common.constant.RollConstants;
import com.mask.roll.common.controller.BaseController;
import com.mask.roll.common.util.JwtUtils;
import com.mask.roll.common.util.RollResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-03-13
 */
@RestController
@RequestMapping("/order")
@CrossOrigin
@Api(tags = "订单管理")
@Slf4j
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    /**
     * 生成订单
     * @param courseId
     * @param request
     * @return
     */
    @PostMapping("/create/{courseId}")
    @ApiOperation("生成订单")
    public RollResponse saveOrder(@ApiParam("课程id") @PathVariable String courseId, HttpServletRequest request) {

        String userWrapper = getUserWrapper(request);

        //创建订单，返回订单号
        String orderNo =
                orderService.createOrders(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return RollResponse.result(ServiceEnums.ORDER_CREATE_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.ORDER_CREATE_SUCCESS.getMessage(),orderNo);
    }

    /**
     * 根据订单id查询订单信息
     * @param orderId
     * @return
     */
    @GetMapping("/get/info/{orderId}")
    @ApiOperation("查询订单")
    public RollResponse getOrderInfo(@ApiParam("订单id") @PathVariable String orderId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        Order order = orderService.getOne(wrapper);
        log.info("用户生成订单信息成功:{}",order);
        return RollResponse.result(ServiceEnums.ORDER_QUERY_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.ORDER_QUERY_SUCCESS.getMessage(),order);
    }

    /**
     * 根据课程id和用户id查询订单状态
     * @param courseId
     * @param memberId
     * @return
     */
    @GetMapping("/is/buy/course/{courseId}/{memberId}")
    @ApiOperation("查询课程支付状态")
    public RollResponse isBuyCourse(
            @ApiParam("课程id") @PathVariable("courseId") String courseId,
            @ApiParam("用户id") @PathVariable("memberId") String memberId){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        wrapper.eq("status","1");
        int count = orderService.count(wrapper);
        if (count > 0) {
            return RollResponse.result(ServiceEnums.ORDER_MEMBER_COURSE_PAID.getCode(),
                    RollConstants.SUCCESS,ServiceEnums.ORDER_MEMBER_COURSE_PAID.getMessage());
        }else {
            return RollResponse.result(ServiceEnums.ORDER_MEMBER_COURSE_UNPAID.getCode(),
                    RollConstants.ERROR,ServiceEnums.ORDER_MEMBER_COURSE_UNPAID.getMessage());
        }

    }
}

